let { projectRootPath } = require("../../util/path/index.js");
let {
  executeSql,
  transaction,
  getTotal,
  getByPagination,
  removeByUuid,
  getAll,
} = require(`${projectRootPath}/mysql/index.js`);
let { getUuid } = require(`${projectRootPath}/util/uuid/index.js`);
let { getCurrentDatetime } = require(
  `${projectRootPath}/util/datetime/index.js`,
);

// 上传业主
async function uploadOwner(data) {
  let { list } = data;
  return await transaction(async (connection) => {
    let sqlPrev = `insert into OwnerTable (uuid, name, groupName, address, homeType, homeStatus, decorationStatus, liveStatus, oweStatus, ownerType, 
                             departmentQuestion, ownerQuestion, payStyle, payMonth, opDatetime) values `;
    for (let i = 0; i < list.length; i++) {
      let tempSqlArray = [];
      for (let j = 0; j < list[i].length; j++) {
        let [name, groupName, address, homeType, homeStatus] = list[i][j];
        tempSqlArray.push(
          `('${getUuid()}', '${name.trim()}', '${groupName.trim()}', '${address.trim()}', '${homeType.trim()}', '${homeStatus.trim()}', '2', '1', '1', 'B 一般客户', '[[]]', '', '', '[]', '${getCurrentDatetime()}')`,
        );
      }
      let sql = sqlPrev + tempSqlArray.join(",");
      await executeSql(connection, sql);
    }
  });
}
// 创建业主
async function createOwner(data) {
  let {
    name,
    address,
    groupName,
    homeType,
    homeStatus,
    decorationStatus,
    liveStatus,
    ownerType,
    oweStatus,
    payStyle,
    payMonth,
    departmentQuestion,
    ownerQuestion,
  } = data;
  return await transaction(async (connection) => {
    let sql = `insert into OwnerTable (uuid, name, address, groupName, homeType, homeStatus, decorationStatus, liveStatus, ownerType, oweStatus, 
                        departmentQuestion, ownerQuestion, payStyle, payMonth, opDatetime) 
                values ('${getUuid()}', '${name}', '${address}', '${groupName}', '${homeType}', '${homeStatus}', '${decorationStatus}', '${liveStatus}',
                            '${ownerType}', '${oweStatus}', '${departmentQuestion}', '${ownerQuestion}', '${payStyle}', '${payMonth}', '${getCurrentDatetime()}')`;
    await executeSql(connection, sql);
  });
}
// 获取员工分页并通过uuid
async function getOwnerByPaginationAndUuid(data) {
  let { uuid, pageSize, currentPage } = data;
  let allWhere = [];
  if (uuid) {
    let where = `uuid='${uuid}'`;
    allWhere.push(where);
  }
  if (allWhere.length > 0) {
    allWhere = "where " + allWhere.join(` and `);
  }
  return await transaction(async (connection) => {
    let list = await getByPagination(
      connection,
      "OwnerTable",
      "opDatetime",
      allWhere,
      pageSize,
      currentPage,
    );
    let total = await getTotal(
      connection,
      "OwnerTable",
      "opDatetime",
      allWhere,
    );
    return { list, total };
  });
}
// 获取员工分页数据
async function getOwnerByPagination(data) {
  let {
    pageSize,
    currentPage,
    name,
    address,
    groupName,
    homeType,
    homeStatus,
    decorationStatus,
    liveStatus,
    ownerType,
    oweStatus,
    payStyle,
    payMonth,
  } = data;
  let allWhere = [];
  if (name) {
    let where = `name='${name}'`;
    allWhere.push(where);
  }
  if (address) {
    let where = `address='${address}'`;
    allWhere.push(where);
  }
  if (groupName) {
    let where = `groupName='${groupName}'`;
    allWhere.push(where);
  }
  if (homeType) {
    let where = `homeType='${homeType}'`;
    allWhere.push(where);
  }
  if (homeStatus) {
    let where = `homeStatus='${homeStatus}'`;
    allWhere.push(where);
  }
  if (decorationStatus) {
    let where = `decorationStatus='${decorationStatus}'`;
    allWhere.push(where);
  }
  if (liveStatus) {
    let where = `liveStatus='${liveStatus}'`;
    allWhere.push(where);
  }
  if (ownerType) {
    let where = `ownerType='${ownerType}'`;
    allWhere.push(where);
  }
  if (oweStatus) {
    let where = `oweStatus='${oweStatus}'`;
    allWhere.push(where);
  }
  if (payStyle) {
    let where = `payStyle='${payStyle}'`;
    allWhere.push(where);
  }
  if (payMonth.length > 0) {
    let tempWhere = [];
    for (let i = 0; i < payMonth.length; i++) {
      let payM = payMonth[i];
      let where = `payMonth like '%"${payM}"%'`;
      tempWhere.push(where);
    }
    tempWhere = `(${tempWhere.join(" or ")})`;
    allWhere.push(tempWhere);
  }
  if (allWhere.length > 0) {
    allWhere = "where " + allWhere.join(` and `);
  }
  return await transaction(async (connection) => {
    let list = await getByPagination(
      connection,
      "OwnerTable",
      "opDatetime",
      allWhere,
      pageSize,
      currentPage,
    );
    let total = await getTotal(
      connection,
      "OwnerTable",
      "opDatetime",
      allWhere,
    );
    return { list, total };
  });
}
// 通过 uuid 编辑业主
async function editOwnerByUuid(data) {
  let {
    uuid,
    name,
    address,
    groupName,
    homeType,
    homeStatus,
    decorationStatus,
    liveStatus,
    ownerType,
    oweStatus,
    payStyle,
    payMonth,
    departmentQuestion,
    ownerQuestion,
  } = data;
  return await transaction(async (connection) => {
    let sql = `update OwnerTable set name='${name}', address='${address}', groupName='${groupName}', homeType='${homeType}',
                            homeStatus='${homeStatus}', decorationStatus='${decorationStatus}', liveStatus='${liveStatus}',
                            ownerType='${ownerType}', oweStatus='${oweStatus}', payStyle='${payStyle}', payMonth='${payMonth}', 
                            departmentQuestion='${departmentQuestion}', ownerQuestion='${ownerQuestion}'
                             where uuid='${uuid}'`;
    await executeSql(connection, sql);
  });
}
// 通过 uuid 删除业主
async function removeOwnerByUuid(data) {
  let { uuid } = data;
  return await transaction(async (connection) => {
    await removeByUuid(connection, "OwnerTable", uuid);
    let sql = `delete from PayTable where ownerUuid='${uuid}'`;
    await executeSql(connection, sql);
  });
}
// 获取全部业主
async function getOwnerByAll() {
  return await transaction(async (connection) => {
    let list = await getAll(connection, "OwnerTable", "opDatetime");
    return { list };
  });
}

// 获取员工分页数据且通过问题列表
async function getOwnerByPaginationAndQuestion(data) {
  let {
    pageSize,
    currentPage,
    name,
    address,
    groupName,
    homeType,
    homeStatus,
    decorationStatus,
    liveStatus,
    ownerType,
    oweStatus,
    payStyle,
    payMonth,
    departmentQuestion,
  } = data;
  let allWhere = [];

  if (departmentQuestion.length > 0) {
    let tempWhere = [];
    for (let i = 0; i < departmentQuestion.length; i++) {
      let que = departmentQuestion[i];
      if (que.length !== 3) {
        continue;
      }
      let where = `departmentQuestion like '%${JSON.stringify(que)}%'`;
      tempWhere.push(where);
    }
    if (tempWhere.length > 0) {
      tempWhere = tempWhere.join(" or ");
      tempWhere = `(${tempWhere})`;
      allWhere.push(tempWhere);
    }
  }

  if (name) {
    let where = `name='${name}'`;
    allWhere.push(where);
  }
  if (address) {
    let where = `address='${address}'`;
    allWhere.push(where);
  }
  if (groupName) {
    let where = `groupName='${groupName}'`;
    allWhere.push(where);
  }
  if (homeType) {
    let where = `homeType='${homeType}'`;
    allWhere.push(where);
  }
  if (homeStatus) {
    let where = `homeStatus='${homeStatus}'`;
    allWhere.push(where);
  }
  if (decorationStatus) {
    let where = `decorationStatus='${decorationStatus}'`;
    allWhere.push(where);
  }
  if (liveStatus) {
    let where = `liveStatus='${liveStatus}'`;
    allWhere.push(where);
  }
  if (ownerType) {
    let where = `ownerType='${ownerType}'`;
    allWhere.push(where);
  }
  if (oweStatus) {
    let where = `oweStatus='${oweStatus}'`;
    allWhere.push(where);
  }
  if (payStyle) {
    let where = `payStyle='${payStyle}'`;
    allWhere.push(where);
  }
  if (payMonth.length > 0) {
    let tempWhere = [];
    for (let i = 0; i < payMonth.length; i++) {
      let payM = payMonth[i];
      let where = `payMonth like '%"${payM}"%'`;
      tempWhere.push(where);
    }
    tempWhere = `(${tempWhere.join(" or ")})`;
    allWhere.push(tempWhere);
  }
  if (allWhere.length > 0) {
    allWhere = "where " + allWhere.join(` and `);
  }
  return await transaction(async (connection) => {
    let list = await getByPagination(
      connection,
      "OwnerTable",
      "opDatetime",
      allWhere,
      pageSize,
      currentPage,
    );
    let total = await getTotal(
      connection,
      "OwnerTable",
      "opDatetime",
      allWhere,
    );
    return { list, total };
  });
}

module.exports = {
  uploadOwner,
  createOwner,
  getOwnerByPagination,
  editOwnerByUuid,
  removeOwnerByUuid,
  getOwnerByAll,
  getOwnerByPaginationAndQuestion,
  getOwnerByPaginationAndUuid,
};
